// OptionsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "calendar.h"
#include "OptionsDlg.h"
#include "CalendarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COptionsDlg dialog


COptionsDlg::COptionsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COptionsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COptionsDlg)
	//}}AFX_DATA_INIT
	m_lOptions = 0;

}


void COptionsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COptionsDlg)
	DDX_Control(pDX, IDC_OPTIONS, m_opt);
	//}}AFX_DATA_MAP

	if(!pDX->m_bSaveAndValidate)
	{
		CImageList list;

		list.Create(IDB_OPTIONS_BMPS,16,1,RGB(255,0,255));
		m_opt.SetImageList(list);

		getOptions();

		m_opt.AddGroup(IDOS_CALENDAR,0,0);

		m_opt.AddCheck(IDOS_SAVEWIDTH, 
			(m_lOptions & IDO_SAVEWIDTH) == IDO_SAVEWIDTH, 1);

		m_opt.AddCheck(IDSO_SAVEPOSITION, 
			(m_lOptions & IDO_SAVEPOSITION) == IDO_SAVEPOSITION, 1);

		m_opt.AddCheck(IDSO_LAUNCHONSTART, 
			(m_lOptions & IDO_LAUNCHONSTART) == IDO_LAUNCHONSTART, 1);

//		m_opt.AddCheck(IDOS_MINIMIZEINTRAY, 
//			(m_lOptions & IDO_MINIMIZEINTRAY) == IDO_MINIMIZEINTRAY, 1);

//		m_opt.AddCheck(IDOS_STARTMINIMIZED, 
//			(m_lOptions & IDO_STARTMINIMIZE) == IDO_STARTMINIMIZE, 1);

		m_opt.AddCheck(IDOS_DISPLAYDAYWEEK, 
			(m_lOptions & IDO_DISPLAYDAYWEEK) == IDO_DISPLAYDAYWEEK, 1);

		m_opt.AddCheck(IDOS_DISPLAYHEX, 
			(m_lOptions & IDO_DISPLAYINHEX) == IDO_DISPLAYINHEX, 1);
		
		m_opt.AddGroup(IDSO_NOTIFY,1,1);

		m_opt.AddRadio(IDSO_NOTIFY_1, 
			(m_lOptions & IDO_NOTIFY_1) == IDO_NOTIFY_1, 2);

		m_opt.AddRadio(IDSO_NOTIFY_2, 
			(m_lOptions & IDO_NOTIFY_2) == IDO_NOTIFY_2, 2);

		m_opt.AddRadio(IDSO_NOTIFY_3, 
			(m_lOptions & IDO_NOTIFY_3) == IDO_NOTIFY_3, 2);

		m_opt.AddRadio(IDSO_NOTIFY_4, 
			(m_lOptions & IDO_NOTIFY_4) == IDO_NOTIFY_4, 2);

		m_opt.AddRadio(IDSO_NOTIFY_5, 
			(m_lOptions & IDO_NOTIFY_5) == IDO_NOTIFY_5, 2);

		m_opt.AddGroup(IDOS_MONITOR,0,2);

		m_opt.AddCheck(IDOSM_LAUNCHONSTART, 
			(m_lOptions & IDOM_LAUNCHONSTART) == IDOM_LAUNCHONSTART, 1);

		m_opt.AddCheck(IDOSM_SAVEPOSITION, 
			(m_lOptions & IDOM_SAVEPOSITION) == IDOM_SAVEPOSITION, 1);

	}
}

BEGIN_MESSAGE_MAP(COptionsDlg, CDialog)
	//{{AFX_MSG_MAP(COptionsDlg)
//	ON_BN_CLICKED(IDC_BTN_FOLDER, OnBtnFolder)
	ON_WM_CLOSE()
	ON_NOTIFY(OPN_SELCHANGE,IDC_OPTIONS,OnOptionsSelChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COptionsDlg message handlers
BOOL COptionsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	return TRUE;
}

/*
void COptionsDlg::OnBtnFolder() 
{
	CString text = getDownloadFolder();

	CShellOpenFolder of;

	if(of.OpenFolderDlg(text, GetSafeHwnd()))
	{
		m_Folder.SetWindowText(text);

		reg.SetUserEntry(REG_APP, REG_DOWNLOADFOLDER, text);
	}
}
*/

void COptionsDlg::OnOptionsSelChange(NMHDR *pNMHDR,LRESULT *pResult)
{
	NMOPTIONSPANEL *pInfo=(NMOPTIONSPANEL *)pNMHDR;
	CString					strReport;

	LPCSTR	lpszKey = "Microsoft\\Windows\\CurrentVersion\\Run", 
			lpszSubkey = "calendar";

	switch(pInfo->idButton)
	{
	case IDSO_LAUNCHONSTART:
		{
			if(pInfo->bChecked) 
			{
				m_lOptions = m_lOptions | IDO_LAUNCHONSTART;

				GetParent()->SendMessage(WM_SETUPTRAYICON, 0, 1);
			}
			else 
			{
				m_lOptions = m_lOptions & ~IDO_LAUNCHONSTART;

				GetParent()->SendMessage(WM_SETUPTRAYICON, 0, 0);
			}
		}
		break;

	case IDOSM_LAUNCHONSTART:
		{
			if(pInfo->bChecked) 
			{
				m_lOptions = m_lOptions | IDOM_LAUNCHONSTART;

			 	char pathBuff[_MAX_PATH] = "\x0";
	
				GetModuleFileName( GetModuleHandle(NULL), pathBuff, sizeof(pathBuff) );

				strcat( pathBuff, " /monitoring" );

				reg.SetUserEntry(lpszKey, lpszSubkey, pathBuff);
			}
			else 
			{
				m_lOptions = m_lOptions & ~IDOM_LAUNCHONSTART;

				reg.DeleteUserEntry(lpszKey, lpszSubkey);
			}
		}
		break;

	case IDOS_SAVEWIDTH:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_SAVEWIDTH;
		else
			m_lOptions = m_lOptions & ~IDO_SAVEWIDTH;

		break;

	case IDOS_MINIMIZEINTRAY:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_MINIMIZEINTRAY;
		else
			m_lOptions = m_lOptions & ~IDO_MINIMIZEINTRAY;

		break;

	case IDOS_STARTMINIMIZED:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_STARTMINIMIZE;
		else
			m_lOptions = m_lOptions & ~IDO_STARTMINIMIZE;

		break;

	case IDOS_DISPLAYDAYWEEK:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_DISPLAYDAYWEEK;
		else
			m_lOptions = m_lOptions & ~IDO_DISPLAYDAYWEEK;

		break;

	case IDOS_DISPLAYHEX:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_DISPLAYINHEX;
		else
			m_lOptions = m_lOptions & ~IDO_DISPLAYINHEX;

		break;

	case IDSO_NOTIFY_1:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_NOTIFY_1;
		else
			m_lOptions = m_lOptions & ~IDO_NOTIFY_1;

		break;

	case IDSO_NOTIFY_2:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_NOTIFY_2;
		else
			m_lOptions = m_lOptions & ~IDO_NOTIFY_2;

		break;

	case IDSO_NOTIFY_3:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_NOTIFY_3;
		else
			m_lOptions = m_lOptions & ~IDO_NOTIFY_3;

		break;

	case IDSO_NOTIFY_4:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_NOTIFY_4;
		else
			m_lOptions = m_lOptions & ~IDO_NOTIFY_4;

		break;

	case IDSO_NOTIFY_5:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_NOTIFY_5;
		else
			m_lOptions = m_lOptions & ~IDO_NOTIFY_5;

		break;

	case IDOSM_SAVEPOSITION:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDOM_SAVEPOSITION;
		else
			m_lOptions = m_lOptions & ~IDOM_SAVEPOSITION;

		break;

	case IDSO_SAVEPOSITION:

		if(pInfo->bChecked)
			m_lOptions = m_lOptions | IDO_SAVEPOSITION;
		else
			m_lOptions = m_lOptions & ~IDO_SAVEPOSITION;

		break;
	}

	reg.SetUserEntry(REG_APP, REG_OPTIONS, m_lOptions);
}

LONG COptionsDlg::getOptions()
{
	m_lOptions = IDO_SAVEWIDTH | IDOM_SAVEPOSITION |
				 IDO_MINIMIZEINTRAY | IDO_DISPLAYDAYWEEK |
				 IDO_NOTIFY_4 | IDO_SAVEPOSITION | IDOM_LAUNCHONSTART;
	
	if(!reg.GetUserEntry(REG_APP, REG_OPTIONS, m_lOptions))
		reg.SetUserEntry(REG_APP, REG_OPTIONS, m_lOptions);

	return m_lOptions;
}

void COptionsDlg::OnClose() 
{
	CDialog::OnClose();
}

void COptionsDlg::OnOK() 
{
	CDialog::OnOK();
}

int COptionsDlg::getDaysMonitor()
{
	getOptions();

	if((m_lOptions & IDO_NOTIFY_1) == IDO_NOTIFY_1)
		return 1;

	if((m_lOptions & IDO_NOTIFY_2) == IDO_NOTIFY_2)
		return 3;

	if((m_lOptions & IDO_NOTIFY_3) == IDO_NOTIFY_3)
		return 5;

	if((m_lOptions & IDO_NOTIFY_4) == IDO_NOTIFY_4)
		return 7;

	if((m_lOptions & IDO_NOTIFY_5) == IDO_NOTIFY_5)
		return 10;

	return 7;
}
